<?php
require($_SERVER['DOCUMENT_ROOT']."/whmcs/dbconnect.php");

$path = "http://".$_SERVER['HTTP_HOST']."/whmcs/admin/clientssummary.php";
$action_path = "http://".$_SERVER['HTTP_HOST']."/whmcs/modules/admin/seller_manager/ajax.php";

/*************/
function get_user_role($userid)
{
	$rs = mysql_query("SELECT id, roleid FROM tbladmins WHERE id = ".intval($userid));
	$row = mysql_fetch_array($rs);
	return $row['roleid'] == 1 ? true : false;	
}

function view_tracking($viewcbosaler, $action_path)
{
	echo $query = "SELECT mod_salertracking.id, clientid, CONCAT(lastname,' ', firstname) AS fullname FROM mod_salertracking JOIN tblclients ON tblclients.id = mod_salertracking.clientid WHERE userid = ".$viewcbosaler." GROUP BY clientid;";
	$rs = mysql_query($query);
	
	$clients = "";
	
	if (mysql_num_rows($rs) > 0)
	{
		$clients .= "<div class='tablebg'><table border='0' width='100%' class='datatable'>
			<tr><th>ID</th><th>Customer Name</th><th>Comment</th>";
		
		if (get_user_role($_SESSION['adminid']))
			$clients .= "<th>Action</th>";
			
		$clients .= "</tr>";
		while ($row = mysql_fetch_array($rs))
		{
			$clients .= "<tr>
					<td>#".$row['clientid']."</td>
					<td><a href='clientssummary.php?userid=".$row['clientid']."'>".$row['fullname']."</a></td>
					<td>".$row['comment']."</td>";
			
			if (get_user_role($_SESSION['adminid']))
				$clients .= "<td><a href='".$action_path."?trackid=".$row['id']."&action=del"."' value='".$row['id']."' class='deletetrack'><img src='images/delete.gif' alt='Delete' width='16' height='16' class='absmiddle' id='intellisearchcancel' /></a></td>";				
			
			$clients .= "</tr>";
		}
		$clients .= '</table></div>';
	}
	else
	{
		$clients .= "<div class='infobox'>No Data Found</div>";
	}
	
	return $clients;
}

if (isset($_POST['type']))
{
	if ($_POST['type'] == 'addtracking' && $_POST['addcbosaler'] != "")
	{
		//check exist client belong to user? true ->update, false -> insert
		$query = "SELECT clientid FROM mod_salertracking WHERE clientid = ".intval($_POST['cboclient']).";";
		
		if (!mysql_num_rows(mysql_query($query)))
		{
			$query = "INSERT INTO mod_salertracking VALUES('',".$_REQUEST['addcbosaler'].",".$_REQUEST['cboclient'].",'".$_REQUEST['comment']."');";
			if (mysql_query($query))	
				echo $msg = "Tracking added successful";
			exit();
		}
		else
		{
			$query = "UPDATE mod_salertracking set userid = ".intval($_POST['addcbosaler']).";";
			if(mysql_query($query))
				echo $msg = "Update saleperson successful";
			exit();
		}	
		exit();
	}
	else if ($_POST['type'] == 'viewtracking' && $_POST['viewcbosaler'] != "")
	{		
		echo view_tracking($_POST['viewcbosaler'], $action_path);
	}
}

if (isset($_REQUEST['action']))
{
	$delquery = mysql_query("DELETE FROM mod_salertracking WHERE id = ".intval($_REQUEST['trackid']).";");	
	if($delquery)	
		echo view_tracking($_POST['viewcbosaler'], $action_path);
}
?>
<script>	
	$().ready(function(){	
		$("a.deletetrack").click(function(){
			if(confirm("Delete this tracking?"))
				$.ajax({
					url: "<?php echo $action_path; ?>",
					type: "post",
					dataType: "text",
					data: "trackid="+$(this).attr('value')+"&action=del&viewcbosaler=<?php echo $_POST['viewcbosaler'] ?>",
					success: function(data){						
						$(".resultitems").html(data);
					},
					error: function(){alert('error')}
				});			
			return false;		
		});
	});
</script>